#include "StdAfx.h"
#include "AR.h"


CAR::CAR(void)
{
	mAttack = 0.05;
	mRelease = 0.9;
	mDuration = 0.0;
	mTime = 0.0;
	mSource = NULL;
}


CAR::~CAR(void)
{
}

void CAR::SetSource( CAudioNode *aSource )
{
	mSource = aSource;
}

void CAR::Start()
{
	mTime = 0.0;
	// This sets the attack and release values as a function of
	// the duration so we don't need to set for every tone.
	double lBps = 1 / ( GetBPM() / 60 );
	mAttack = mDuration * mAttack * lBps;
	mRelease = mDuration * mRelease * lBps;

}

bool CAR::Generate()
{
	double lRamp = 0;
	double lBps = 1 / ( GetBPM() / 60 );
	double lActual = mDuration * lBps;
	if (mTime < mAttack)
		lRamp = mTime / mAttack;
	else if( mTime > lActual )
	{
		lRamp = 0;
		return false;
	}
	else if( mTime > mRelease )
		lRamp = ( lActual - mTime ) / ( lActual - mRelease);
	else
		lRamp = 1;

	mFrame[0] = mSource->Frame(0) * lRamp;
	mFrame[1] = mFrame[0];
	mTime += GetSamplePeriod();
	
	return true;
}